<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>组合继承</title>
</head>
<body>
  <script>
    function SuperType(name) {
      this.name = name;
      this.colors = ["red", "blue", "green"];
    }

    SuperType.prototype.sayName = function() {
      console.log(this.name);
    };

    function SubType(name, age) {
      // 继承属性
      SuperType.call(this, name);

      this.age = age;
    }

    // 继承方法
    SubType.prototype = new SuperType();

    SubType.prototype.sayAge = function() {
      console.log(this.age);
    };

    let instance1 = new SubType("Nicholas", 29);
    instance1.colors.push("black");
    console.log(instance.colors);  // "red,blue,green,black"
    instance1.sayName();  // "Nicholas"
    instance1.sayAge();  // 29

    let instance2 = new SubType("Greg", 27);
    console.log(instance2.colors);  // "red,blue,green"
    instance2.sayName();  // "Greg"
    instance2.sayAge();  // 27
  </script>
</body>
</html>